<?php
//require_once  __DIR__ . '/includes/authenticate.php';

if (isset($_POST['season']) && isset($_POST['date'])) {
	

include_once './db_bb_connect.php';

$season = $_POST['season'];
$date = $_POST['date'];


$sql = "SELECT teams.playerID, users.firstname
	FROM teams 
	JOIN users 
	ON teams.playerID = users.user_key 
	WHERE teams.season = '$season'";


$result = $db->query($sql);

while ($row = $result->fetch_assoc()){

$playerID = $row['playerID'];
$firstname = $row['firstname'];

foreach ($_POST as $key => $value) {
	if ($key == $playerID) {
		$ftscored = $value;
		$sql2 = "INSERT INTO trainingft (playerID, season, date, ftscored) 
	VALUES ('$playerID', '$season', '$date', '$ftscored')";
	if ($db->query($sql2)) {
		//echo "<br><center><strong>Training Presence has been succesfully added! </strong></center>";
	} else {
		echo "<br><center>Error: " . $sql2 . "<br>" . $db->error . "</center>";
		}
	} else if ($key == "total$playerID") {
			$fttotal = $value;

			$sql2 = "UPDATE trainingft 
			SET fttotal = '$fttotal' 
	WHERE playerID = '$playerID' AND date = '$date'";

			if ($db->query($sql2)) {
			} else {
				echo "<br><center>Error: " . $sql2 . "<br>" . $db->error . "</center>";
			}
		}
	}
}
echo "<br><center><strong>Training FT data has been succesfully added! </strong></center>";

}



?>